using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Common
{
    public class DispartPage
    {
        public static SqlDataReader GetDispartPage(string TableName, string OrderByCol, int PageSize, int PageIndex, bool OrderType, string strWhere, SqlConnection connection,string PK)
        {
            SqlParameter[] parms ={
                new SqlParameter("@tablename",SqlDbType.VarChar,100),
                new SqlParameter("@fieldlist",SqlDbType.VarChar,4000),
                new SqlParameter("@orderfield",SqlDbType.VarChar,100),
                new SqlParameter("@keyfield",SqlDbType.VarChar,100),
                new SqlParameter("@PageIndex",SqlDbType.Int,4),
                new SqlParameter("@PageSize",SqlDbType.Int,4),
                new SqlParameter("@strWhere",SqlDbType.VarChar,1000),
                new SqlParameter("@OrderType",SqlDbType.Bit,1)

             };
            parms[0].Value = TableName;
            parms[1].Value = "*";
            parms[2].Value = OrderByCol;
            parms[3].Value = PK;
            parms[4].Value = PageIndex;
            parms[5].Value = PageSize;
            parms[6].Value = strWhere;
            parms[7].Value = OrderType;

            return RunProcedure("GetPagingRecord", parms, connection);
        }





        public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters,SqlConnection connection)
        {
            SqlDataReader returnReader;
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.CommandType = CommandType.StoredProcedure;
            returnReader = command.ExecuteReader();
            return returnReader;
        }

        private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
            return command;
        }

        public static int TotalRecord(string TableName, int PageSize, string strWhere, SqlConnection connection)
        {

            string strcmd = "select count(1) from " + TableName;
            if (strWhere.Trim() != "")
            {
                strcmd += (" where" + strWhere);
            }
            SqlCommand cmd = new SqlCommand(strcmd, connection);
            int TR = Convert.ToInt32(cmd.ExecuteScalar());
            if (TR % PageSize == 0)
            {
                return TR / PageSize;
            }
            else
            {
                return TR / PageSize + 1;
            }
        }

        public static int TotalRecord(string TableName, string strWhere, SqlConnection conn)
        {
            string strcmd = "select count(1) from " + TableName;
            if(strWhere.Trim()!="")
            {
                strcmd+=(" where "+strWhere);
            }

            SqlCommand cmd = new SqlCommand(strcmd, conn);
            return  Convert.ToInt32(cmd.ExecuteScalar());
        }
    }

    
}
